//
//  NSDate+time.m
//  App
//
//  Created by huanchi on 15/11/18.
//  Copyright © 2015年. All rights reserved.
//

#import "NSDate+Time.h"

@implementation NSDate (Time)

+(NSString *)currentTimeWithFormatter:(NSString *)formatter{
    NSDate *date = [NSDate date];
    return [date timeWithFormatter:formatter];
}

+(NSString *)currentSystemMillisecond{
    NSDateFormatter *formatter = [[NSDateFormatter alloc ] init];
    [formatter setDateFormat:@"YYYYMMddHHmmsss"];
    NSDate *nowDate = [NSDate date];
    NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)[nowDate timeIntervalSince1970]];
    return timeSp;
}

-(NSString *)timeWithFormatter:(NSString *)formatter{
    NSDateFormatter *form = [[NSDateFormatter alloc]init];
    form.dateFormat = formatter;
    return [form stringFromDate:self];
}

+(NSDate *)dateFromTime:(NSString *)time formatter:(NSString *)formatter{
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    format.dateFormat = formatter;
    return [format dateFromString:time];
}

-(NSString *)timeDescription{
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:self];
    NSInteger minute = interval/60;
    if (minute == 0)  return @"刚刚";
    if (minute> 0 && minute <= 30) return [NSString stringWithFormat:@"%ld分钟前",(long)minute];
    
    //今天
    NSString *time = [NSDate currentTimeWithFormatter:@"YYYY/MM/dd"];
    NSDate *today = [NSDate dateFromTime:time formatter:@"YYYY/MM/dd"];
    NSTimeInterval todayInterval = [[NSDate date] timeIntervalSinceDate:today];
    if (interval < todayInterval) {
        return [self timeWithFormatter:@"HH:mm"];
    }
    //今天以前
    return [self timeWithFormatter:@"YYYY-MM-dd HH:mm"];
}
@end
